package org.NooLab.itexx.storage;

import java.util.ArrayList;


import org.NooLab.utilities.datatypes.DataTypes;

import com.iciql.Db;

/**
 * 
 * maps a are too limited, but this serves the same purpose
 * 
 * 
 *
 */
public class DbFieldRequests {

	ArrayList<DbFieldRequest> items = new ArrayList<DbFieldRequest>();
	Db iciDb;
	
	Conditions conditions = new Conditions();
	
	// ------------------------------------------------------------------------
	public DbFieldRequests(Db db) throws Exception{
		if (db==null){
			throw(new Exception("ici db must not be null."));
		}
		iciDb = db;
	}
	// ------------------------------------------------------------------------
	
	public DbFieldRequest getfieldRequest(int index){
		DbFieldRequest item = null;
		
		if ((index>=0) && (index<items.size()) ){
			item = items.get(index);
		}
			
		return item ;
	}
	
	public ArrayList<DbFieldRequest> getItems(){
		return items;
	}

	public void clear() {
		items.clear();
	}

	
	
	private void _put(DbFieldRequest item, String fieldname, Object fieldvalue, int fieldtype){
		
		item.fieldName = fieldname;
		item.fieldValue = fieldvalue ;
		item.fieldType  = fieldtype ; 
		
		items.add(item);
	}
	
	public void put(String fieldname, Long fieldvalue) {
		DbFieldRequest item = new DbFieldRequest();

		int _type = DataTypes.getTypeId( fieldvalue.getClass().getName() );

		_put(item, fieldname, fieldvalue, _type);
	}
	
	public void put(String fieldname, Double fieldvalue) {
		DbFieldRequest item = new DbFieldRequest();

		int _type = DataTypes.getTypeId( fieldvalue.getClass().getName() );

		_put(item, fieldname, fieldvalue, _type);
	}

	public void put(String fieldname, Integer fieldvalue) {
		DbFieldRequest item = new DbFieldRequest();

		int _type = DataTypes.getTypeId( fieldvalue.getClass().getName() );

		_put(item, fieldname, fieldvalue, _type);
	}
	
	public void put(String fieldname, String fieldvalue) {
		DbFieldRequest item = new DbFieldRequest();

		int _type = DataTypes.getTypeId( fieldvalue.getClass().getName() );

		_put(item, fieldname, fieldvalue, _type);
	}
	
	public void put(String fieldname, Object fieldvalue) {
		DbFieldRequest item = new DbFieldRequest();

		_put(item, fieldname, fieldvalue, 0);
	}

	public int size() {
		return items.size();
	}

	public Object getfieldRequestValue(String keystring) {
		// 
		Object obj = null;
		DbFieldRequest item ;
		for (int i=0;i<items.size();i++){
			
			item = items.get(i);
			if (item.fieldName.toLowerCase().contentEquals(keystring)){
				obj = item.fieldValue ;
				break;
			}
			
		}//i ->
			
			
		return obj;
	}

	
}
